﻿using CubicFWNet.Application;
using CubicFWNet.Application.SystemManager;
using CubicFWNet.Common;
using CubicFWNet.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CubicFWNet.Data.ViewModal;


namespace CubicFWNet.Mvc.Controllers
{
    public class HomeController : ControllerBase
    {
        private ModuleApp _app;
        private TaskRemindApp _taskRemindApp;
        private OrderApp _orderApp;

        public HomeController()
        {
            _app = new ModuleApp();
            _taskRemindApp = new TaskRemindApp();
            _orderApp = new OrderApp();
        }

        public ActionResult Index()
        {
            //更新提醒数据
            _taskRemindApp.UpdateAll();

            //获取提醒列表
            List<TaskRemindView> ls = _taskRemindApp.GetRemindListByUser(operatorModel.UserId);

            //销售员提醒数量
            DateTime _start = Convert.ToDateTime("2017-1-1");
            DateTime _end = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            ViewBag.SubCount = _orderApp.GetApplyCount(operatorModel.UserId, _start, _end.AddDays(1), 6);//确认下单
            ViewBag.PriCount = _orderApp.GetApplyCount(operatorModel.UserId, _start, _end.AddDays(1), 501);//确认最终价

            ViewBag.TaskList = ls;

            return View();
        }

        [ChildActionOnly]
        public PartialViewResult Navbar()
        {
            var bar = GetNavBar();
            ViewBag.NavBar = bar;
            if (CurrentModule != null)
            {
                ViewBag.CurrentNav = CurrentModule.Kind==1? CurrentModule.Id.ToString():CurrentModule.ParentId.ToString();
            }                
            return PartialView("_Navigation");
        }

        public string GetNavBar()
        {
            var modules = base.UserModules.Where(c=>c.Kind==1);
            IEnumerable<TreeItem<Module>> moduleItem = modules.OrderBy(a => a.SortNo).GenerateTree(c => c.Id, c => c.ParentId);
            var nav = BuilderNavBar(moduleItem);
            return nav;
        }

        public string BuilderNavBar(IEnumerable<TreeItem<Module>> modules)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var module in modules)
            {
                if (module.Children.Any())
                {
                    sb.Append("<li class=\"treeview\" id=\"" + module.Item.Id.ToString() + "\">\r\n");
                    sb.Append("<a href=\"#\">\r\n");
                    sb.Append("<i class=\"fa " + module.Item.IconName + "\" ></i>\r\n");
                    sb.Append("<span>\r\n");
                    sb.Append("" + module.Item.Name + "\r\n");
                    sb.Append("</span>\r\n");
                    sb.Append("\r\n");
                    sb.Append("<span class=\"pull-right-container\">\r\n");
                    sb.Append("<i class=\"fa fa-angle-left pull-right\"></i>\r\n");
                    sb.Append("</span>\r\n");
                    sb.Append("</a>\r\n");
                    sb.Append("\r\n");
                    sb.Append("<ul class=\"treeview-menu\">");
                    sb.Append(BuilderNavBar(module.Children));
                    sb.Append("</ul>");
                    sb.Append("</li>\r\n");
                }
                else
                {
                    sb.Append("<li class=\"treeview\"  id=\"" + module.Item.Id.ToString() + "\">\r\n");
                    sb.Append("<a href=\"" + module.Item.Url + "\" >\r\n");
                    sb.Append("<i class=\"fa " + module.Item.IconName + "\"></i>\r\n");
                    sb.Append("<span> " + module.Item.Name + " </span>\r\n");
                    sb.Append("</a>\r\n");
                    sb.Append("\r\n");
                    sb.Append("</li>\r\n");
                }
            }
            return sb.ToString();
        }

    }
}